home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / PIL / IptcImagePlugin.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-13  |  6.0 KB  |  242 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __version__ = '0.3'
  5. import Image
  6. import ImageFile
  7. import os
  8. import tempfile
  9. COMPRESSION = {
  10.     1: 'raw',
  11.     5: 'jpeg' }
  12. PAD = chr(0) * 4
  13.  
  14. def i16(c):
  15.     return ord(c[1]) + (ord(c[0]) << 8)
  16.  
  17.  
  18. def i32(c):
  19.     return ord(c[3]) + (ord(c[2]) << 8) + (ord(c[1]) << 16) + (ord(c[0]) << 24)
  20.  
  21.  
  22. def i(c):
  23.     return i32(PAD + c[-4:])
  24.  
  25.  
  26. def dump(c):
  27.     for i in c:
  28.         print '%02x' % ord(i),
  29.     
  30.     print 
  31.  
  32.  
  33. class IptcImageFile(ImageFile.ImageFile):
  34.     format = 'IPTC'
  35.     format_description = 'IPTC/NAA'
  36.     
  37.     def getint(self, key):
  38.         return i(self.info[key])
  39.  
  40.     
  41.     def field(self):
  42.         s = self.fp.read(5)
  43.         if not len(s):
  44.             return (None, 0)
  45.         
  46.         tag = (ord(s[1]), ord(s[2]))
  47.         if ord(s[0]) != 28 and tag[0] < 1 or tag[0] > 9:
  48.             raise SyntaxError, 'invalid IPTC/NAA file'
  49.         
  50.         size = ord(s[3])
  51.         if size > 132:
  52.             raise IOError, 'illegal field length in IPTC/NAA file'
  53.         elif size == 128:
  54.             size = 0
  55.         elif size > 128:
  56.             size = i(self.fp.read(size - 128))
  57.         else:
  58.             size = i16(s[3:])
  59.         return (tag, size)
  60.  
  61.     
  62.     def _is_raw(self, offset, size):
  63.         return 0
  64.         self.fp.seek(offset)
  65.         (t, sz) = self.field()
  66.         if sz != size[0]:
  67.             return 0
  68.         
  69.         y = 1
  70.         while None:
  71.             (t, s) = self.field()
  72.             if t != (8, 10):
  73.                 break
  74.             
  75.             if s != sz:
  76.                 return 0
  77.             
  78.             y = y + 1
  79.             continue
  80.             return y == size[1]
  81.  
  82.     
  83.     def _open(self):
  84.         while None:
  85.             offset = self.fp.tell()
  86.             (tag, size) = self.field()
  87.             if not tag or tag == (8, 10):
  88.                 break
  89.             
  90.             if size:
  91.                 self.info[tag] = self.fp.read(size)
  92.                 continue
  93.             self.info[tag] = None
  94.             continue
  95.             layers = ord(self.info[(3, 60)][0])
  96.             component = ord(self.info[(3, 60)][1])
  97.             if self.info.has_key((3, 65)):
  98.                 id = ord(self.info[(3, 65)][0]) - 1
  99.             else:
  100.                 id = 0
  101.         if layers == 1 and not component:
  102.             self.mode = 'L'
  103.         elif layers == 3 and component:
  104.             self.mode = 'RGB'[id]
  105.         elif layers == 4 and component:
  106.             self.mode = 'CMYK'[id]
  107.         
  108.         self.size = (self.getint((3, 20)), self.getint((3, 30)))
  109.         
  110.         try:
  111.             compression = COMPRESSION[self.getint((3, 120))]
  112.         except KeyError:
  113.             raise IOError, 'Unknown IPTC image compression'
  114.  
  115.         if tag == (8, 10):
  116.             if compression == 'raw' and self._is_raw(offset, self.size):
  117.                 self.tile = [
  118.                     (compression, (offset, size + 5, -1), (0, 0, self.size[0], self.size[1]))]
  119.             else:
  120.                 self.tile = [
  121.                     ('iptc', (compression, offset), (0, 0, self.size[0], self.size[1]))]
  122.         
  123.  
  124.     
  125.     def load(self):
  126.         if len(self.tile) != 1 or self.tile[0][0] != 'iptc':
  127.             return ImageFile.ImageFile.load(self)
  128.         
  129.         (type, tile, box) = self.tile[0]
  130.         (encoding, offset) = tile
  131.         self.fp.seek(offset)
  132.         outfile = tempfile.mktemp()
  133.         o = open(outfile, 'wb')
  134.         if encoding == 'raw':
  135.             o.write('P5\n%d %d\n255\n' % self.size)
  136.         
  137.         while None:
  138.             (type, size) = self.field()
  139.             if type != (8, 10):
  140.                 break
  141.             
  142.             while size > 0:
  143.                 s = self.fp.read(min(size, 8192))
  144.                 if not s:
  145.                     break
  146.                 
  147.                 o.write(s)
  148.                 size = size - len(s)
  149.             continue
  150.             
  151.             try:
  152.                 self.im = Image.core.open_ppm(outfile)
  153.             except:
  154.                 im = Image.open(outfile)
  155.                 im.load()
  156.                 self.im = im.im
  157.             finally:
  158.                 
  159.                 try:
  160.                     os.unlink(outfile)
  161.                 except:
  162.                     pass
  163.  
  164.  
  165.             return None
  166.  
  167.  
  168. Image.register_open('IPTC', IptcImageFile)
  169. Image.register_extension('IPTC', '.iim')
  170.  
  171. def getiptcinfo(im):
  172.     import TiffImagePlugin
  173.     import JpegImagePlugin
  174.     import StringIO
  175.     data = None
  176.     if isinstance(im, IptcImageFile):
  177.         return im.info
  178.     elif isinstance(im, JpegImagePlugin.JpegImageFile):
  179.         
  180.         try:
  181.             app = im.app['APP13']
  182.             if app[:14] == 'Photoshop 3.0\x00':
  183.                 app = app[14:]
  184.                 offset = 0
  185.                 while app[offset:offset + 4] == '8BIM':
  186.                     offset = offset + 4
  187.                     code = JpegImagePlugin.i16(app, offset)
  188.                     offset = offset + 2
  189.                     name_len = ord(app[offset])
  190.                     name = app[offset + 1:offset + 1 + name_len]
  191.                     offset = 1 + offset + name_len
  192.                     if offset & 1:
  193.                         offset = offset + 1
  194.                     
  195.                     size = JpegImagePlugin.i32(app, offset)
  196.                     offset = offset + 4
  197.                     if code == 1028:
  198.                         data = app[offset:offset + size]
  199.                         break
  200.                     
  201.                     offset = offset + size
  202.                     if offset & 1:
  203.                         offset = offset + 1
  204.                         continue
  205.         except (AttributeError, KeyError):
  206.             pass
  207.         except:
  208.             None<EXCEPTION MATCH>(AttributeError, KeyError)
  209.         
  210.  
  211.     None<EXCEPTION MATCH>(AttributeError, KeyError)
  212.     if isinstance(im, TiffImagePlugin.TiffImageFile):
  213.         
  214.         try:
  215.             (type, data) = im.tag.tagdata[TiffImagePlugin.IPTC_NAA_CHUNK]
  216.         except (AttributeError, KeyError):
  217.             pass
  218.         except:
  219.             None<EXCEPTION MATCH>(AttributeError, KeyError)
  220.         
  221.  
  222.     None<EXCEPTION MATCH>(AttributeError, KeyError)
  223.     if data is None:
  224.         return None
  225.     
  226.     
  227.     class FakeImage:
  228.         pass
  229.  
  230.     im = FakeImage()
  231.     im.__class__ = IptcImageFile
  232.     im.info = { }
  233.     im.fp = StringIO.StringIO(data)
  234.     
  235.     try:
  236.         im._open()
  237.     except (IndexError, KeyError):
  238.         pass
  239.  
  240.     return im.info
  241.  
  242.